/**
 * @Title: LoginController.java
 * @Package com.leesche.zcsp.node.controller
 * @Description:()
 * @author dennies yang
 * @date 2016年9月21日 下午2:58:12
 * @version V1.0
 */
package com.leesche.zcsp.node.web.controller.web.v2.forget;

import com.leesche.zcsp.node.app.v1.timer.PhoneCodeCacheTimer;
import com.leesche.zcsp.node.app.v1.timer.VaildPhone;
import com.leesche.zcsp.node.utils.security.QEncodeUtil;
import com.leesche.zcsp.node.utils.web.SessionManager;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.template.LoginTemplate;
import com.leesche.zcsp.node.web.model.SysUser;
import com.leesche.zcsp.node.web.vo.Account;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * @author dennies yang
 * @version V1.0
 * @Title: LoginController.java
 * @Package com.leesche.zcsp.node.controller
 * @Description:(登录界面)
 * @date 2016年9月21日 下午3:02:45
 */
@Controller
@RequestMapping(value = "/v2/forget")
public class WebForgetController extends LoginTemplate {

    private final Logger logger = Logger.getLogger(WebForgetController.class);

    private static final String FOGGET_VIEW = "web/v2/forget/fogetpassword";

    @RequestMapping(value = {"/showview"})
    public String showLoginView() {
        return FOGGET_VIEW;
    }

    /**
     * @param sysUser 登录的账户
     * @Title: login(Account accout)
     * @Description: TODO(PC端用户登录入口)
     * @author Jerry
     * @date 2016-10-15 12:44:49
     */
    @ResponseBody
    @RequestMapping(value = "/fogetpassword", method = RequestMethod.POST)
    public Map<String, Object> editPassowrd(SysUser sysUser, String smscode, String imgcode) {
        try {

            if (!SessionManager.getSessionAttribute(Constants.LOGIN_CODE).equals(imgcode)) {
                return WebResult.failure("验证码输入错误").put("ERROR", "imgcode").toMap();
            } else if (smscode == null || "".equals(smscode)) {
                return WebResult.failure("短信验证码不能为空").put("ERROR", "smscode").toMap();
            }

            VaildPhone validPhone = PhoneCodeCacheTimer.get(smscode);
            if (validPhone == null) {
                return WebResult.failure("短信验证码错误，请重新输入").put("ERROR", "smscode").toMap();
            } else if (validPhone.getValidTime() < new Date().getTime()) {
                PhoneCodeCacheTimer.remove(smscode);
                return WebResult.failure("短信验证码已过期，请重新获取").put("ERROR", "smscode").toMap();
            } else if (!validPhone.getPhone().equals(sysUser.getMobile())) {
                return WebResult.failure("与获取短信的手机号码一致").put("ERROR", "mobile").toMap();
            } else {
                SysUser user = new SysUser();
                user.setUsername(sysUser.getUsername());
                user = sysUserService.findUserByUsername(user);
                if (user == null) {
                    return WebResult.failure("用户名不已存在").put("ERROR", "username").toMap();
                } else if (!user.getMobile().equals(sysUser.getMobile())) {
                    return WebResult.failure("手机号与注册时的手机号不一致").put("ERROR", "mobile").toMap();
                } else {
                    sysUser.setUserid(user.getUserid());
                    sysUser.setAdminid(user.getUserid());
                    sysUser.setPassword(QEncodeUtil.md5Encrypt(sysUser.getPassword()));
                    sysUser.setUpdatetime(new Date());
                }
            }

            if(sysUserService.updateByPrimaryKeySelective(sysUser) > 0){
                PhoneCodeCacheTimer.remove(smscode);
                return WebResult.success().toMap();
            }else{
                return WebResult.failure("找回密码失败，请联系管理员").toMap();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    /**
     * @Title: quit()
     * @Description: TODO(PC端用户登出入口)
     * @author Jerry
     * @date 2016-10-15 12:44:49
     */
    @ResponseBody
    @RequestMapping(value = "/signout", method = RequestMethod.DELETE)
    public Map<String, Object> quit() {
        try {
            if (privilegeService.getCurrentPrincipal() != null) {
                SessionManager.destroySession();
                return WebResult.success().info("操作成功").toMap();
            } else {
                return WebResult.failure("用户未登录，请先登录").toMap();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

}
